package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Movethehistory;
import cn.wolfcode.crm.domain.PotentialStudent;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.MovethehistoryMapper;
import cn.wolfcode.crm.mapper.PotentialStudentMapper;
import cn.wolfcode.crm.query.MovethehistoryQueryObject;
import cn.wolfcode.crm.service.IMovethehistoryService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service
public class MovethehistoryServiceImpl implements IMovethehistoryService {
    @Autowired
    private MovethehistoryMapper movethehistoryMapper;
    @Autowired
    private PotentialStudentMapper PsMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void saveOrUpdate(PotentialStudent po,Long inputId) {
        if(inputId == null) {
            Movethehistory move = new Movethehistory();
            move.setStudentName(po.getName());
            move.setDate(new Date());
            move.setSource(po.getSalesman());
            move.setStudentQq(po.getQq());
            move.setStudentTel(po.getTel());
            move.setTarget(UserContext.getUserContext());
            movethehistoryMapper.insert(move);
            PsMapper.updateByUserName(UserContext.getUserContext(), po.getId());
        } else {
            Movethehistory move = new Movethehistory();
            move.setStudentName(po.getName());
            move.setDate(new Date());
            move.setSource(po.getSalesman());
            move.setStudentQq(po.getQq());
            move.setStudentTel(po.getTel());
            Employee emp = employeeMapper.selectByPrimaryKey(inputId);
            move.setTarget(emp);
            movethehistoryMapper.insert(move);
            PsMapper.updateByUserName(emp, po.getId());
        }
    }

    @Override
    public PageResult getAll(MovethehistoryQueryObject qo) {
        int count = movethehistoryMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Movethehistory> data = movethehistoryMapper.queryList(qo);
        return new PageResult(count, data);
    }


}
